package ilvonen.ihstatistics.repository;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
/*
 * This Adapter creates the database for your application.
 * You must define your select, insert, update, delete methods for each table in the tables own class,
 * for example in DBTeams.java. There we have select, insert, update, delete methods for table 'teams'
 */
public class DBAdapter {

	private static final String DATABASE_NAME = "MyDatabase"; //choose your database name
	private static final int DATABASE_VERSION = 1;
	
	private final Context context;
	
	private DatabaseHelper DBHelper;
	private SQLiteDatabase db;
	/*
	 * CLASS STATUS: READY!
	 */
	public DBAdapter(Context ctx){
		this.context = ctx;
		DBHelper = new DatabaseHelper(context);
	}
	
	public SQLiteDatabase getDatabase(){
		return db;
	}
	
	private static class DatabaseHelper extends SQLiteOpenHelper{
		DatabaseHelper(Context context) {
			// TODO Auto-generated constructor stub
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			/*
			 * creates the database if it doesn't already exist
			 */
			try{
				/*
				 * Create a new file (class) for each table.
				 * In each class define CREATE STATEMENT in SQL and a create method.
				 * See DBTeams.java for example.
				 * Then call for this funtion for all your table classes which you want to create.
				 * db.execSQL(new YourDBTableClass().create());
				 */
				db.execSQL(new DBDefaultRoster().create());
				db.execSQL(new DBGames().create());
				db.execSQL(new DBLeagues().create());
				db.execSQL(new DBLines().create());
				db.execSQL(new DBPenaltyReasons().create());
				db.execSQL(new DBPlayers().create());
				db.execSQL(new DBPositions().create());
				db.execSQL(new DBRoles().create());
				db.execSQL(new DBRoster().create());
				db.execSQL(new DBSeasons().create());
				db.execSQL(new DBSeasonTypes().create());
				db.execSQL(new DBStats().create());
				db.execSQL(new DBStatTypes().create());
				db.execSQL(new DBTeams().create());
				
				
				
				
				
				//Log.d("Testi","Taulut luotu");
			} catch (SQLException e){
				e.printStackTrace();
			}
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// Use in case you need to upgrade your database
		}
	}
	
	/*
	 * opens the database
	 */
	public DBAdapter open() throws SQLException{
		db = DBHelper.getWritableDatabase();
		return this;
	}
	
	/*
	 * closes the database
	 */
	public void close(){
		DBHelper.close();
	}
	
	/*
	 * insert records to any table. This function is called from the table class.
	 */
	public long insertRecord(String TABLE_NAME, ContentValues initialValues){
		return db.insert(TABLE_NAME, null, initialValues);
	}
	
	/*
	 * delete record from any table. define deletable record with id.
	 */
	public boolean deleteRecord(String TABLE_NAME, String IDENTIFY_COLUMN, long ROW_ID){
		return db.delete(TABLE_NAME, IDENTIFY_COLUMN + "=" + ROW_ID, null) > 0;
	}
	
	/*
	 * retrieves all records from the table. 
	 */
	public Cursor getAllRecords(String TABLE_NAME, String[] COLUMN_NAMES){
		return db.query(TABLE_NAME, COLUMN_NAMES, null, null, null, null, null);
	}
	/*
	 * Retrieve all records by id from the table.
	 */
	public Cursor getAllRecordsById(String TABLE_NAME, String[] COLUMN_NAMES, String IDENTIFY_COLUMN, long ROW_ID){
		return db.query(TABLE_NAME, COLUMN_NAMES, IDENTIFY_COLUMN + "=" + ROW_ID, null, null, null, null);
	}
	/*
	 * retrieves a particular record
	 * COLUMN_NAMES = which columns you want to retrieve?
	 * IDENTIFY_COLUMN = which column is used as an identifier?
	 */
	public Cursor getRecord(String TABLE_NAME, String[] COLUMN_NAMES, String IDENTIFY_COLUMN, long ROW_ID) throws SQLException{
		Cursor mCursor = db.query(true, TABLE_NAME, COLUMN_NAMES, IDENTIFY_COLUMN + "=" + ROW_ID, null, null, null, null, null);
		if(mCursor != null){
			mCursor.moveToFirst();
		}
		return mCursor;
	}
	
	/*
	 * update a record
	 */
	public boolean updateRecord(String TABLE_NAME, ContentValues initialValues, String IDENTIFY_COLUMN, long ROW_ID){
		return db.update(TABLE_NAME, initialValues, IDENTIFY_COLUMN + "=" + ROW_ID, null) > 0;
	}
}
